<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Admin\Traits\AdminTrait;
use App\Http\Requests\Admin\BaseRequest;
use App\Models\Admin\GoodsDatum;
use App\Models\Admin\GoodsStandard;
use App\Models\Admin\GoodsStandardSpu;
use Illuminate\Validation\Rule;
use Saas,Auth,DB;

class GoodsDatumController extends AdminController
{
    use AdminTrait;

    protected $model;
    protected $unique_id = '';

    public function __construct(GoodsDatum $model)
    {
        parent::__construct();
        $this->model = $model;
    }

    /**
     * 定义验证规则
     */
    protected $validate_rule = [

    ];

    /**
     * 定特殊的验证规则
     */
    protected function rules(){

        return [
            'title' => [
                'required',
                Rule::unique('goods_datum')->where(function ($query) {
                    $type = request('type',1);
                    $query->where('type',$type)->where(function ($query){
                        $query->where('company_id',0);
                    });
                })->ignore($this->unique_id)
            ],
        ];
    }

    /**
     * 定义验证错误信息
     */
    protected $err_msg = [
        'title.required' => '内容不能为空',
        'title.unique'=> '内容已经存在了'
    ];

    /**
     * 定义列表排序
     */
    protected $orderBy = [
        'id' => 'asc',
    ];

    protected $title = [
        1 => '计量单位',
        2 => '标签',
        3 => '不可互调SKU属性',
        4 => '品牌',
    ];

    public function index(BaseRequest $request)
    {
        $brand=GoodsDatum::getDatum(4);
        $unit=GoodsDatum::getDatum(1);
        $tag=GoodsDatum::getDatum(2);
        $attribute=GoodsDatum::getDatum(3);
        return response()->json(['errcode'=>0,'data'=>['unit'=>$unit,'tag'=>$tag,'attribute'=>$attribute,'brand'=>$brand],'errmsg'=>"ok"]);
    }

//    protected function afterData($info,$requestData){
//    }

    protected function setRequestField($arr,$type=false){
        //过滤字段
        if(isset($arr['_token'])) unset($arr['_token']);
        if (isset($arr['sign'])) unset($arr['sign']);
        if (isset($arr['timeStamp'])) unset($arr['timeStamp']);
        return $arr;
    }



    protected function checkDelete($id)
    {
        $info = $this->model->find($id);
        if (empty($info)){
            return $this->error(['-1','资料不存在']);
        }
        $type=$info->type;
        if($type==1)//单位
        {
            $is_spu_name=GoodsStandardSpu::where('spu_name',$id)->count();
            $is_unit=GoodsStandardSpu::where('unit',$id)->count();
            if($is_spu_name || $is_unit)
            {
                return ['errcode'=>40009,'errmsg'=>'该单位正在使用，不能删除'];
            }
        }
        elseif($type==3)//不可互调
        {
            $is_spu_name=GoodsStandardSpu::where('spu_name',$id)->count();
            if($is_spu_name)
            {
                return ['errcode'=>40009,'errmsg'=>'该属性正在使用，不能删除'];
            }
        }
        elseif($type==4)//不可互调
        {
            $is_use=GoodsStandard::where('brand',$id)->count();
            if($is_use)
            {
                return ['errcode'=>40009,'errmsg'=>'品牌正在使用，不能删除'];
            }
        }
        return true;
    }



    public function datumList(BaseRequest $request)
    {
        $list = $this->model->where('status',1)->where(function ($query){
            $query->where('company_id',0);
        })->get()->groupBy('type')->toArray();

        $data = [];
        foreach ($list as $key => $val){
            //$arr['id'] = $key;
            $arr['type'] = $key;
            $arr['title'] = $this->title[$key];
            $arr['isSelect'] = $this->isSelect($key,1);
            foreach ($val as $k => $v){
                $val[$k]['isSelect'] = $this->isSelect($key,2,$v['id']);
            }
            $arr['selfNext'] = $val;
            $data[] = $arr;
        }

        return response()->json(['errcode'=>0,'data'=>['list'=>$data],'errmsg'=>"ok"]);
    }

    private function isSelect($type,$class=1,$id=0)
    {
        $datum = GoodsDatum::getDatum($type);
        if ($class == 1 && !empty($datum)) {
            return true;
        }

        if ($class == 2 && !empty($datum)){
            $list = json_decode($datum->datums,true);
            foreach ($list as $k => $v){
                if ($v['id'] == $id){
                    return true;
                }
            }
        }

        return false;
    }
}
